<template>
  <div class="list" ref="wrapper">
    <div>
      <div class="area">
          <div class="title border-topbottom">当前城市</div>
          <div class="button-list">
              <div class="button-city">
                  <div class="button-city-con">{{this.currentCity}}</div>
              </div>
          </div>
      </div>
      <div class="area">
          <div class="title border-topbottom">热门城市</div>
          <div class="button-list">
            <div
              class="button-city"
              v-for="item in Hot"
              :key="item.id"
              @click="handleHotCity(item.name)"
            >
              <div class="button-city-con">{{item.name}}</div>
            </div>
          </div>
      </div>
      <div class="area" v-for="(area, key) in List" :key="key" :ref="key">
          <div class="title border-topbottom">{{key}}</div>
          <div class="button-list">
            <div
              class="button-item border-bottom"
              v-for="inner in area"
              :key="inner.id"
              @click="handleHotCity(inner.name)"
            >{{inner.name}}</div>
          </div>
      </div>
    </div>
  </div>
</template>
<script>
import BScroll from 'better-scroll'
import { mapState, mapActions } from 'vuex'
export default{
  name: 'cityList',
  props: {
    List: Object,
    Hot: Array,
    letter: String
  },
  methods: {
    ...mapActions(['handleHotCity']),
    handleHotCity (val) {
      this.$store.dispatch('hotCityChange', val)
      this.$router.push('/')
    }
  },
  computed: {
    ...mapState({
      currentCity: 'city'
    })
  },
  mounted () {
    this.scroll = new BScroll(this.$refs.wrapper, {
      click: true
    })
  },
  watch: {
    letter () {
      if (this.letter) {
        const element = this.$refs[this.letter][0]
        this.scroll.scrollToElement(element)
      }
    }
  }
}
</script>
<style lang="stylus" scoped>
  @import "~styles/varibles.styl";
  .border-topbottom
    &:before
      border-color: #ccc
    &:after
      border-color: #ccc
  .list
    position: absolute
    top: 1.6rem
    left: 0
    right: 0
    bottom: 0
    overflow: hidden
    .title
      background: #eee
      line-height: .54rem
      padding-left: .2rem
      color: #666
      font-size: .26rem
    .button-list
      overflow: hidden
      padding: .1rem .6rem .1rem .1rem
      .button-city
        width: 33.33%
        float: left
        .button-city-con
          border: 1px solid #ccc
          padding: .1rem 0
          margin: .1rem
          text-align: center
          border-radius: .08rem
      .button-item
        line-height: .76rem
        padding-left: .15rem
</style>
